package com.llxy.worker.service.impl;

import com.llxy.common.core.domain.entity.SysDept;
import com.llxy.common.core.domain.entity.SysDictData;
import com.llxy.system.mapper.SysDeptMapper;
import com.llxy.system.mapper.SysDictDataMapper;
import com.llxy.worker.domain.Homemaking;
import com.llxy.worker.domain.School;
import com.llxy.worker.domain.Train;
import com.llxy.worker.dto.SupportWorkerDTO;
import com.llxy.worker.dto.TrainDTO;
import com.llxy.worker.mapper.HomemakingMapper;
import com.llxy.worker.mapper.SchoolMapper;
import com.llxy.worker.mapper.TrainMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.llxy.worker.domain.SupportWorker;
import com.llxy.worker.mapper.SupportWorkerMapper;
import com.llxy.worker.service.SupportWorkerService;

import java.util.ArrayList;
import java.util.List;

@Service
public class SupportWorkerServiceImpl implements SupportWorkerService{

    @Autowired
    private SupportWorkerMapper supportWorkerMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private TrainMapper trainMapper;

    @Autowired
    private HomemakingMapper homemakingMapper;

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return supportWorkerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(SupportWorker record) {
        return supportWorkerMapper.insert(record);
    }

    @Override
    public int insertSelective(SupportWorker record) {
        return supportWorkerMapper.insertSelective(record);
    }

    @Override
    public SupportWorkerDTO selectByPrimaryKey(Integer id) {
        return this.SupportWorkerDetailInfo(supportWorkerMapper.selectByPrimaryKey(id));
    }

    @Override
    public int updateByPrimaryKeySelective(SupportWorker record) {
        return supportWorkerMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SupportWorker record) {
        return supportWorkerMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<SupportWorkerDTO> selectSupportWorkerList(SupportWorker supportWorker) {
        List<SupportWorkerDTO> supportWorkerDTOList = new ArrayList<>();
        //条件查询
        List<SupportWorker> supportWorkerList = supportWorkerMapper.selectSupportWorkerList(supportWorker);
        supportWorkerList.forEach(item -> supportWorkerDTOList.add(this.SupportWorkerDetailInfo(item)));
        return supportWorkerDTOList;
    }

    @Override
    public Integer selectSupportWorkerListCount(SupportWorker supportWorker) {
        return supportWorkerMapper.selectSupportWorkerListCount(supportWorker);
    }

    @Override
    public int insertSupportWorker(SupportWorker supportWorker) {
        return supportWorkerMapper.insertSelective(supportWorker);
    }

    @Override
    public int updateRecommendStatus(Integer id, String status) {
        return supportWorkerMapper.updateRecommendStatus(id,status);
    }

    @Override
    public int updateOutstandingStatus(Integer id, String status) {
        return supportWorkerMapper.updateOutstandingStatus(id,status);
    }

    @Override
    public List<SupportWorker> selectStatusIsZoneList(Long userId) {
        return supportWorkerMapper.selectStatusIsZoneList(userId);
    }

    @Override
    public SupportWorker selectByUserId(Long userId) {
        return supportWorkerMapper.selectByUserId(userId);
    }

    @Override
    public List<SupportWorker> selectAllStatusIsZoneList() {
        return supportWorkerMapper.selectAllStatusIsZoneList();
    }

    public SupportWorkerDTO SupportWorkerDetailInfo(SupportWorker supportWorker){
        //学历信息字典
        List<SysDictData> sysEducations = sysDictDataMapper.selectDictDataByType("sys_education");
        //护工类型字典
        List<SysDictData> sysSupportWorkerTypes = sysDictDataMapper.selectDictDataByType("sys_support_worker");

        SupportWorkerDTO supportWorkerDTO = new SupportWorkerDTO();
        BeanUtils.copyProperties(supportWorker,supportWorkerDTO);

        //学历信息
        sysEducations.forEach(edu -> {
            if (supportWorker.getEducationId().toString().equals(edu.getDictValue())){
                supportWorkerDTO.setEducation(edu.getDictLabel());
            }
        });
        //护工类型信息
        sysSupportWorkerTypes.forEach(type -> {
            if (supportWorker.getType().toString().equals(type.getDictValue())){
                supportWorkerDTO.setTypeName(type.getDictLabel());
            }
        });

        TrainDTO trainDTO = new TrainDTO();
        //根据培训id查询所属培训信息
        Train train = trainMapper.selectByPrimaryKey(supportWorker.getTrainId());
        //根据学校id查询所属学校信息
        School school = schoolMapper.selectByPrimaryKey(train.getSchoolId());
        //根据专业id查询专业名称
        sysSupportWorkerTypes.forEach(type -> {
            if (train.getSpecialityId().equals(type.getDictValue())){
                trainDTO.setSpecialityName(type.getDictLabel());
            }
        });
        //根据部门id查询所属部门信息
        SysDept sysDept = sysDeptMapper.selectDeptById(train.getDeptId());
        trainDTO.setSchool(school);
        trainDTO.setSysDept(sysDept);
        BeanUtils.copyProperties(train,trainDTO);

        //根据家政id查询所属家政信息
        Homemaking homemaking = homemakingMapper.selectByPrimaryKey(supportWorker.getHomemakingId());

        supportWorkerDTO.setTrainDTO(trainDTO);
        supportWorkerDTO.setHomemaking(homemaking);

        return supportWorkerDTO;
    }

}
